<?
    final class Principio_Model_TypeInCompanyExtTemplateMapper extends Principio_Mapper
    {
        protected static $db_table = null;
        protected static function getDbTableName() { return "CompanyTypeExtTemplate"; }



        public static function fetch(Principio_Model_TypeInCompany $model_type_in_company)
        {
            try
            {
                $values = self::getDBTable()->fetchAll(
                    self::getDBTable()
                        ->select()
                        ->where("type_id=?", $model_type_in_company->getModelType()->getID())
                        ->where("company_id=?", $model_type_in_company->getModelCompany()->getID())
                );

                return Principio_Model_Template::makeCollection($values->toArray());
            }
            catch (Exception $ex)
            {
                return self::reportException($ex);
            };
        }


        public static function insert(Principio_Model_TypeInCompany $model_type_in_company)
        {
            for ($i = 0; $i < $model_type_in_company->getModelType()->getExts() + Principio_Model_Templatable::TEMPLATE_SHIFT; $i++)
            {
                self::getDBTable()->insert([
                    'type_id' => $model_type_in_company->getModelType()->getID(),
                    'company_id' => $model_type_in_company->getModelCompany()->getID(),
                    'ext_number' => $i,
                    'template' => ""
                ]);

                $model_type_in_company->getTemplates()->addItem(new Principio_Model_Template($i));
            };

            return true;
        }


        public static function delete(Principio_Model_TypeInCompany $model_type_in_company)
        {
            try
            {
                self::getDBTable()->delete([
                    'type_id=?' => $model_type_in_company->getModelType()->getID(),
                    'company_id=?' => $model_type_in_company->getModelCompany()->getID()
                ]);

                return true;
            }
            catch (Exception $ex)
            {
                self::reportException($ex);
                throw new RuntimeException("Couldn't delete templates.");
            };
        }


        public static function update(Principio_Model_TypeInCompany $model_type_in_company)
        {
            try
            {
                self::getDBTable()->getAdapter()->beginTransaction();

                foreach ($model_type_in_company->getTemplates() as $model_template)
                {
                    self::getDBTable()->update(
                        [
                            'template' => $model_template->getTemplate()
                        ],
                        [
                            'type_id=?' => $model_type_in_company->getModelType()->getID(),
                            'company_id=?' => $model_type_in_company->getModelCompany()->getID(),
                            'ext_number=?' => $model_template->getExtNumber()
                        ]
                    );
                };

                self::getDBTable()->getAdapter()->commit();

                return true;

            }
            catch (Exception $ex)
            {
                self::reportException($ex);
                self::getDBTable()->getAdapter()->rollBack();

                return false;
            };
        }


        /*

        public static function search($string)
        {
            $search_fields = array(
                "template" => "Template"
            );

            $result = parent::searchInDB(self::getDBTable(), $search_fields, $string);

            foreach ($result as $res)
            {
                $type_info = Principio_Model_TypeMapper::get($res["type_id"]);
                $model_search = new Principio_Model_Search($string, "Spa types");
                $menu_data = array("menu_name" => "types", "level" => EXPAND_TYPES, "open_level" => 1);
                $model_search->setId($res["type_id"])->setResultName($type_info->getCaption())->setOpenItemFunctionName("Types")->setParentID(false)->setMenuData($menu_data);
                foreach ($res as $col_name => $value)
                {
                    if (strstr($value, $string) && in_array($col_name, array_keys($search_fields)))
                        $searchable_text[$search_fields[$col_name]] = $value;
                }
                $model_search->setResult($searchable_text);
                $search[$res["type_id"]] = $model_search;
            }
            return $search;


        }
        */
    }
?>